package org.batatasframework.common;

import java.util.Calendar;

import cn.bonoon.kernel.util.StringHelper;

/**
 * 从字符串里解析出年份和月份；支持中文格式的描述等
 * @author jackson
 *
 */
public class MonthlyResolver extends NumberResolver{
	
	private final static int MIN_ANNUAL = 1970;
	private final static int MIN_MONTH = 1;
	private final static int MAX_MONTH = 12;
	private final static int RATE_ANNUAL = 100;
	
	private final int currentAnnual = currentYear();
	
	private int minAnnual = MIN_ANNUAL;
	private int maxAnnual = currentAnnual;
	private int[] annuals;
	private boolean annualExcluded;
	private int[] months;
	private boolean monthExcluded;
	
	private int annual;
	private int month;
	
	private int currentYear(){
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	
	public static MonthlyResolver valueOf(String source){
		return new MonthlyResolver(source);
	}
	
	private boolean checkAnnual(int year){

		if(year < minAnnual || year > maxAnnual){
			message = "年份不正确数值不在指定的范围[" + minAnnual + "-" + maxAnnual + "]内";
			return false;
		}
		
		if(null != annuals && annuals.length > 0){
			if(annualExcluded){
				for(int ea : annuals){
					if(ea == year){
						message = "年份[" + year + "]被指定排除";
						return false;
					}
				}
			}else{
				for(int ia : annuals){
					if(ia == year) return true;
				}
				message = "指定年份里不包含" + year + "年";
				return false;
			}
		}
		
		//检查通过
		return true;
	}

	private boolean checkMonth(int month){
		
		if(MIN_MONTH > month || month > MAX_MONTH){
			message = "日期月份不正确";
			return false;
		}
		
		if(null != months && months.length > 0){
			if(monthExcluded){
				for(int me : months){
					if(me == month){
						message = "月份[" + month + "]被指定排除";
						return false;
					}
				}
			}else{
				for(int mi : months){
					if(mi == month) return true;
				}

				message = "指定月份里不包含" + month + "月";
				return false;
			}
		}
		
		return true;
	}

	public boolean parse(){
		if(StringHelper.isEmpty(source)){
			message = "日期为空";
			return false;
		}
		
		//前4位是年份
		StringBuilder strYear = new StringBuilder();
		char[] chs = source.toCharArray();
		int point = 0;
		for(; point < chs.length; point++){
			if(readNumber(chs[point], strYear)){
				//年份格式默认是4位数
				if(strYear.length() == 4) break;
			}else if(strYear.length() > 1) break;//年份，起码两个数值，如：16、17等
		}
		if(strYear.length() == 2){
			//如果是两位数的，则需要对前面两位进行补齐
			annual = Integer.parseInt(strYear.toString());
			int complete = (currentAnnual / RATE_ANNUAL) * RATE_ANNUAL;//去掉后面两位数
			annual += complete;
			while(annual > maxAnnual) annual -= RATE_ANNUAL;
		}else if(strYear.length() == 4){
			annual = Integer.parseInt(strYear.toString());
		}
		//如果年份只有1或3位数的，将无法被正确解析
		
		StringBuilder strMonth = new StringBuilder();
		point++;
		for(; point < chs.length; point++){
			if(readNumber(chs[point], strMonth)){
				//月份，最多就两位数，也可能是一位数
				if(strMonth.length() == 2) break;
			}else if(strMonth.length() > 0){
				if(strMonth.charAt(0) != '0') break;//有可能是"2017-0 1-01"这种情况
			}
		}
		//可能会是空的,解析成空，如果是填的中文的话
		if (StringHelper.isEmpty(strMonth.toString())) {
			message="日期格式解析异常";
			return false;
		}
		//计算月份
		month = Integer.parseInt(strMonth.toString());
		
		return checkAnnual(annual) && checkMonth(month);
	}
	
	private MonthlyResolver(String source){
		super(source);
	}

	public int getAnnual() {
		return annual;
	}

	public int getMonth() {
		return month;
	}
	
}
